/**
 * @file basic_usage.cpp
 * @brief Basic usage example for CompleteBinaryTree
 */

#include "../complete_binary_tree.hpp"
#include <iostream>
#include <string>

int main() {
    std::cout << "=== Complete Binary Tree - Basic Usage Example ===\n\n";

    // Create a tree with 8 leaf nodes
    // Leaves store strings, internal nodes store integers
    CompleteBinaryTree<std::string, int> tree(8);
    
    std::cout << "Created tree with " << tree.leaf_count() << " leaves\n";
    std::cout << "Internal nodes: " << tree.internal_count() << "\n";
    std::cout << "Total nodes: " << tree.size() << "\n";
    std::cout << "Tree depth: " << tree.depth() << "\n\n";

    // Set leaf values
    std::cout << "Setting leaf values...\n";
    for (size_t i = 0; i < tree.leaf_count(); ++i) {
        tree[i] = "Leaf_" + std::to_string(i);
    }
    
    // Set internal node values
    std::cout << "Setting internal node values...\n";
    for (size_t i = 0; i < tree.internal_count(); ++i) {
        tree.internal_at(i) = static_cast<int>(i * 10);
    }

    // Access elements with bounds checking
    std::cout << "\nAccessing elements:\n";
    try {
        std::cout << "  First leaf: " << tree.at(0) << "\n";
        std::cout << "  Last leaf: " << tree.at(7) << "\n";
        std::cout << "  Root node (internal[0]): " << tree.internal_at(0) << "\n";
    } catch (const std::out_of_range& e) {
        std::cerr << "Error: " << e.what() << "\n";
    }

    // Demonstrate unchecked access
    std::cout << "\nUnchecked access (using operator[]):\n";
    std::cout << "  tree[3] = " << tree[3] << "\n";

    // Resize operations
    std::cout << "\n=== Resize Operations ===\n";
    std::cout << "Resizing tree to 4 leaves...\n";
    tree.resize(4);
    std::cout << "New leaf count: " << tree.leaf_count() << "\n";
    std::cout << "First preserved leaf: " << tree[0] << "\n";

    std::cout << "\nExpanding tree to 16 leaves...\n";
    tree.resize(16);
    std::cout << "New leaf count: " << tree.leaf_count() << "\n";
    std::cout << "New leaves are default-initialized\n";

    // Reserve capacity
    std::cout << "\nReserving capacity for 32 leaves...\n";
    tree.reserve(32);

    // Integrity check
    std::cout << "\n=== Integrity Check ===\n";
    if (tree.validate_integrity()) {
        std::cout << "✓ Tree integrity validated successfully\n";
    } else {
        std::cout << "✗ Tree integrity check failed\n";
    }

    // Clear tree
    std::cout << "\nClearing tree...\n";
    tree.clear();
    std::cout << "Tree is empty: " << (tree.empty() ? "yes" : "no") << "\n";
    std::cout << "Leaf count: " << tree.leaf_count() << "\n";

    std::cout << "\n=== Example Complete ===\n";
    return 0;
}
